<template>
  <div class="youke">
    <Navs left></Navs>
    <Main>
      <template slot="main">
        <div class="news-box">
          <div class="news-list-box">
            <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
              <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                <van-cell :to="'/news/detail?id='+ item.id" v-for="(item,inx) in news" :key="inx">
                  <div class="news-flex">
                    <div class="news-banner">
                      <img :src="item.thumb" alt>
                    </div>
                    <div class="news-info">
                      <p class="news-title">{{item.title}}</p>

                      <p class="news-time">发布时间 {{item.time}}</p>
                    </div>
                  </div>
                </van-cell>
              </van-list>
            </van-pull-refresh>
          </div>
        </div>
      </template>
    </Main>
  </div>
</template>

<script type="text/ecmascript-6">
import { news } from "@/api";
export default {
  //接收父级传递的参数
  props: [],
  //监听数据变化
  watch: {},
  data() {
    return {
      news: [],
      page: 1,
      loading: true,
      finished: true,
      isLoading: true
    };
  },
  //实例初始化之前触发事件
  beforeCreate() {},
  //实例化之后触发事件
  created() {},
  //实时监控data参数数据变化
  computed: {},
  //模板组件
  components: {},
  //执行方法
  methods: {
    newsList() {
      news.newslist({ page: this.page, type: 2 }).then(res => {
        if (res.code == 1000) {
          if (Number(res.count) != 0) {
             this.news = [...this.news, ...res.result];
             this.loading=false
            if (this.news.length== Number(res.count)) {
              this.finished = true;
            }
          } else {
            this.news = [];
            this.finished = true;
            this.loading = false;
          }
        }
      });
    },
    onLoad() {
      this.page += 1;
      this.newsList()
    },
    onRefresh() {
      this.nolist=true
      this.loading=true
      setTimeout(() => {
        this.$toast("刷新成功");
        this.isLoading = false;
        this.page = 1;
        this.news = [];
        this.newsList();
      }, 500);
    }
  },
  mounted() {
    this.newsList();
  }
  //路由退出时候调用
  //beforeRouteLeave(to, from, next){next()}
};
</script>

<style scoped lang="less">
.news-box {
  .news-list-box {
    .news-flex {
      display: flex;
    }
    .news-banner {
      height: 4.5rem;
      width: 7rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.2rem;
      }
    }
    .news-info {
      width: 14rem;
      margin-left: 1rem;

      .news-title {
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2; // 控制多行的行数
        -webkit-box-orient: vertical;
      }

      .news-time {
        margin-top: 0.5rem;
        font-size: 12px;
        color: #afafaf;
      }
    }
  }
}
</style>
